package openjdk.collections.map;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * 这个示例用于展示TreeMap的基本用法，对map 的key 进行降序排序
 * 如果想要对value进行排序，请参考示例TreeMapDemo2
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        Map<String,String> map = new TreeMap<String,String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // desc
                return o2.compareTo(o1);
            }
        });

        map.put("c","x993kadsf");
        map.put("d","d993kadsf");
        map.put("a","z993kadsf");
        map.put("f","f993kadsf");
        map.put("b","b993kadsf");

        Iterator<String> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + " : " + map.get(key));
        }
    }
}
